package com.foxzzz.dynamic.programming;

public class Solution303 {
    public static void main(String[] args) {
        Solution303 solution303 = new Solution303();
        NumArray numArray = solution303.getNumArray(new int[]{-2, 0, 3, -5, 2, -1});

        System.out.println(numArray.sumRange(0, 2));
        System.out.println(numArray.sumRange(2, 5));
        System.out.println(numArray.sumRange(0, 5));

    }

    public NumArray getNumArray(int[] nums) {
        return new NumArray(nums);
    }

    class NumArray {
        int[] dp;
        int[] nums;

        public NumArray(int[] nums) {
            dp = new int[nums.length];
            this.nums = nums;
            for (int i = 0; i < nums.length; i++) {
                dp[i] = nums[i] + (i - 1 >= 0 ? dp[i - 1] : 0);
            }
        }

        public int sumRange(int i, int j) {
            return dp[j] - dp[i] + nums[i];
        }


    }
}
